/**
 * 
 */
package com.wolfpire.subject.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.wolfpire.subject.dao.ProfessionSubjectRelationDao;
import com.wolfpire.system.common.base.dao.impl.BaseHibernateDao;
import com.wolfpire.system.dao.OrgUserRelationDao;
import com.wolfpire.system.model.OrgUserRelation;
import com.wolfpire.system.model.ProfessionSubjectRelation;

/**
 * @author lihd
 *
 */

@Repository("professionSubjectRelationDao")
public class ProfessionSubjectRelationDaoImpl extends BaseHibernateDao<ProfessionSubjectRelation, Long> implements ProfessionSubjectRelationDao {

	public void relate(Long professionId, Long subjectId) {
		//先删除后绑定
		this.deleteBySubjectId(subjectId);
		if (null != professionId) {
			ProfessionSubjectRelation ps = new ProfessionSubjectRelation(professionId,subjectId);
			this.saveOrUpdate(ps);
		}
	}

	private void deleteBySubjectId(Long subjectId) {
		String executeSql = "DELETE FROM t_profession_subject WHERE subject_id = ?";
		this.executeSql(executeSql, subjectId);
		
	}

	public ProfessionSubjectRelation getPSRelation(Long subjectId) {
		Criteria c = createCriteria(Restrictions.eq("subjectId", subjectId));
		Object obj = c.uniqueResult();
		if (null != obj) {
			return (ProfessionSubjectRelation) obj;
		}
		return null;
	}

	public List<ProfessionSubjectRelation> getSPRelations(Long professionId) {
		Criteria c = createCriteria(Restrictions.eq("professionId", professionId), Restrictions.isNotNull("subjectId"));
		return c.list();
	}

}
